package com.marvels.system.permission.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.system.permission.dao.SysOrgDao;
import com.marvels.system.permission.dto.SysOrgDTO;
import com.marvels.system.permission.service.OrgService;

@Service
public class OrgServiceImpl implements OrgService {
	@Autowired
	private SysOrgDao sysOrgDao;

	/**
	 * 获取组织机构树
	 */
	@Override
	public List<SysOrgDTO> getOrgTree()throws MarvelsBusinessException{
		//获取全部组织信息
		List<SysOrgDTO> list = sysOrgDao.selectOrgs();
		List<SysOrgDTO> orgTree = new ArrayList<SysOrgDTO>();
		List<SysOrgDTO> children = null;
		if(list!=null && list.size()>0){
			for(SysOrgDTO org : list){
				if("-1".equals(org.getParentOrgCode())){
					//解析子节点，拼装成树结构
					children = getOrgsByParent(org,list);
					org.setChildren(children);
					//添加根节点
					orgTree.add(org);
				}
			}
		}
		return orgTree;
	}
	
	private List<SysOrgDTO> getOrgsByParent(SysOrgDTO parent,List<SysOrgDTO> allOrgs)throws MarvelsBusinessException{
		List<SysOrgDTO> children = new ArrayList<SysOrgDTO>();
		for(SysOrgDTO org : allOrgs){
			if(parent.getOrgCode().equals(org.getParentOrgCode())){
				children.add(org);
			}
		}
		if(children.size() > 0){
			for(SysOrgDTO child : children){
				child.setChildren(getOrgsByParent(child,allOrgs));
			}
		}
		return children;
	}

	/**
	 * 获取组织节点信息
	 */
	@Override
	public SysOrgDTO getOrgInfo(SysOrgDTO org) throws MarvelsBusinessException {
		if(org==null || org.getOrgId()==0){
			throw new MarvelsBusinessException("组织机构对象为空");
		}
		return sysOrgDao.selectOrg(org);
	}
	
	/**
	 * 
	* @Title: saveOrg 
	* @Description:  新增组织
	* @param org
	* @return int
	* @throws MarvelsBusinessException       
	* @author 李瑶
	* @date 2018年1月22日 下午4:45:34
	 */
	@Override
	public SysOrgDTO saveOrg(SysOrgDTO sysOrg) throws MarvelsBusinessException {
		isOrgCodeEmpty(sysOrg);
		String orgCode = sysOrg.getOrgCode();
		if(isOrgCodeExistence(orgCode)){
			throw new MarvelsBusinessException("组织机构代码"+orgCode+"已存在");
		}
		sysOrg.setCreatedBy(sysOrg.obtainLoginUserName());
		sysOrg.setUpdatedBy(sysOrg.obtainLoginUserName());
		sysOrgDao.insertSelective(sysOrg);
		return sysOrg;
	}
	
	@Override
	public SysOrgDTO saveRootOrg(SysOrgDTO sysOrg) throws MarvelsBusinessException {
		isOrgCodeEmpty(sysOrg);
		sysOrg.setParentOrgCode("-1");
		sysOrg.setParentOrgName(null);
		return saveOrg(sysOrg);
	}
	
	/**
	 * 判断orgCode是否已存在
	 * @param orgCode
	 * @return
	 */
	private boolean isOrgCodeExistence(String orgCode){
		boolean isExisted = false;
		int count = sysOrgDao.selectCountByOrgCode(orgCode);
		if(count > 0){
			isExisted = true;
		}
		return isExisted;
	}
	
	/**
	 * 
	* @Title: delete 
	* @Description:  删除组织
	* @param orgId
	* @return
	* @throws MarvelsBusinessException       
	* @author 李瑶
	* @date 2018年1月22日 下午4:47:15
	 */
	@Override
	public int deleteOrg(SysOrgDTO sysOrg) throws MarvelsBusinessException {
		isOrgCodeEmpty(sysOrg);
		sysOrg.setUpdatedBy(sysOrg.obtainLoginUserName());
		return sysOrgDao.deleteByPrimaryKey(sysOrg);
	}

	/**
	 * 
	* @Title: updateOrg 
	* @Description: 更新组织 
	* @param org 组织实体类
	* @return
	* @throws MarvelsBusinessException       
	* @author 李瑶
	* @date 2018年1月22日 下午4:48:32
	 */
	@Override
	public int updateOrg(SysOrgDTO org) throws MarvelsBusinessException {
		isOrgCodeEmpty(org);
		org.setUpdatedBy(org.obtainLoginUserName());
		return sysOrgDao.updateByPrimaryKeySelective(org);
	}
	
	private void isOrgCodeEmpty(SysOrgDTO org) throws MarvelsBusinessException{
		if(org==null || StringUtils.isEmpty(org.getOrgCode())){
			throw new MarvelsBusinessException("组织机构代码为空");
		}
	}
}
